package com.leetcode.p200;

import java.util.HashMap;

public class Test290 {

    public boolean wordPattern(String pattern, String s) {
        if (s == null || s.isEmpty()) {
            return false;
        }

        String[] array = s.split(" ");

        if (pattern.length() != array.length) {
            return false;
        }

        HashMap<Character, String> char2String = new HashMap<>();
        HashMap<String, Character> string2Char = new HashMap<>();

        for (int i = 0; i < pattern.length(); i++) {
            Character cChar = pattern.charAt(i);
            String value = array[i];

            if (char2String.containsKey(cChar)) {
                if (!char2String.get(cChar).equals(value)) {
                    return false;
                }
            } else {
                char2String.put(cChar, value);
            }

            if (string2Char.containsKey(value)) {
                if (string2Char.get(value) != cChar) {
                    return false;
                }
            } else {
                string2Char.put(value, cChar);
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Test290 test = new Test290();
        System.out.println(test.wordPattern("abba", "dog cat cat dog"));
//        System.out.println(test.wordPattern("abba", "dog dog dog dog"));
    }

}
